# -*- coding:utf-8 -*-
#! python3

from facecat import *
import os

#更新悬浮状态
#views:视图集合
def updateView(views):
	updateViewDefault(views)

#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	onPaintDefault(view, paint, clipRect)

#绘制视图边线
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaintBorder(view, paint, clipRect):
	onPaintBorderDefault(view, paint, clipRect)

#视图的鼠标移动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseMove(view, mp, buttons, clicks, delta):
	onMouseMoveDefault(view, mp, buttons, clicks, delta)
		
#视图的鼠标按下方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseDown(view, mp, buttons, clicks, delta):
	if view.viewType == "chart":
		if len(view.addingPlot) > 0:
			invalidateView(view)
			addPlotDefault(view, True, mp, False, mp)
			view.addingPlot = ""
		else:
			touchDownChart(view, True, mp, False, mp)
			invalidateView(view)
	else:
		onMouseDownDefault(view, mp, buttons, clicks, delta)

#视图的鼠标抬起方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseUp(view, mp, buttons, clicks, delta):
	onMouseUpDefault(view, mp, buttons, clicks, delta)

#视图的鼠标点击方法
#view 视图
#firstTouch:是否第一次触摸 
#firstPoint:第一次触摸的坐标 
#secondTouch:是否第二次触摸 
#secondPoint:第二次触摸的坐标
#clicks 点击次数
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)
	if view.viewType == "plot":
		chart.addingPlot = view.text
	elif view.viewType == "indicator":
		if view.text == "BOLL" or view.text == "MA":
			chart.mainIndicator = view.text
		else:
			chart.showIndicator = view.text
		calcChartIndicator(chart)
		invalidateView(chart)

#视图的鼠标滚动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseWheel(view, mp, buttons, clicks, delta):
	onMouseWheelDefault(view, mp, buttons, clicks, delta)

#视图的键盘按下方法
#view 视图
#value 按键值
def onKeyDown(view, value):
	onKeyDownDefault(view, value)

	#视图的键盘抬起方法
#view 视图
#value 按键值
def onKeyUp(view, value):
	onKeyUpDefault(view, value)

appPaint = FCPaint() #创建绘图对象
appPaint.defaultUIStyle = "dark"
appPaint.onPaint = onPaint 
appPaint.onPaintBorder = onPaintBorder
appPaint.onMouseDown = onMouseDown 
appPaint.onMouseMove = onMouseMove 
appPaint.onMouseUp = onMouseUp
appPaint.onMouseWheel = onMouseWheel
appPaint.onClick = onClick
appPaint.onUpdateView = updateView
appPaint.onKeyDown = onKeyDown
appPaint.onKeyUp = onKeyUp

#消息循环
def WndProc(hwnd,msg,wParam,lParam):
	return WndProcDefault(appPaint,hwnd,msg,wParam,lParam)

#测试第四个层
def testDiv4():
	global chart
	chart.candleDivPercent = 0.3
	chart.volDivPercent = 0.2
	chart.indDivPercent = 0.25
	chart.indDivPercent2 = 0.25
	line1 = BaseShape()
	line1.color = "rgb(255,0,0)"
	line1.divIndex = 3
	line1.shapeName = "line1"
	line1.title = "A"
	line2 = BaseShape()
	line2.color = "rgb(0,255,0)"
	line2.color2 = "rgb(255,255,0)"
	line2.divIndex = 3
	line2.shapeName = "line2"
	line2.title = "B"
	line2.title2 = "C"
	line2.shapeType = "bar"
	line2.style = "2color"
	chart.shapes.append(line1)
	chart.shapes.append(line2)
	for i in range(0, len(chart.data)):
		line1.datas.append(chart.data[i].close)
		if i % 2 == 0:
			line2.datas.append(chart.data[i].low)
		else:
			line2.datas.append(-chart.data[i].low)
		line2.datas2.append(0)
		#line2.datas2.append(chart.data[i].high)
	
#创建窗体
createMainWindow(appPaint, "facecat-py", WndProc)
split = FCSplitLayoutDiv()
split.dock = "fill"
split.size = FCSize(400, 400)
addView(split, appPaint)
chart = FCChart()
chart.leftVScaleWidth = 80
chart.backColor = "rgb(0,0,0)";
chart.borderColor = "rgb(100,100,100)";
chart.textColor = "rgb(255,255,255)";
chart.scaleColor = "rgb(100,100,100)";
chart.crossTipColor = "rgb(50,50,50)";
chart.crossLineColor = "rgb(100,100,100)";
chart.gridColor = "rgba(100,100,100,125)";
chart.indicatorColors = []
chart.indicatorColors.append("rgb(255,255,255)")
chart.indicatorColors.append("rgb(255,255,0)")
chart.indicatorColors.append("rgb(255,0,255)")
chart.indicatorColors.append("rgb(255,0,0)")
chart.indicatorColors.append("rgb(0,255,255)")
chart.indicatorColors.append("rgb(0,255,0)")
chart.indicatorColors.append("rgb(255,255,0)")
chart.indicatorColors.append("rgb(255,255,255)")
addViewToParent(chart, split)
chart.mainIndicator = "MA" 
chart.showIndicator = "MACD" 
layout = FCView()
layout.viewType = "div"
layout.backColor = "rgb(0,0,0)"
addViewToParent(layout, split)
layout.showHScrollBar = True
layout.showVScrollBar = True
layout.allowDragScroll = True
layout.scrollSize = 0
splitter = FCView()
splitter.location = FCPoint(0, 340)
splitter.size = FCSize(400, 1)
addViewToParent(splitter, split)
split.firstView = layout
split.secondView = chart
split.splitter = splitter
split.layoutStyle = "bottomtotop"
split.oldSize = FCSize(400, 400)
plots = []
plots.append("Line")
plots.append("Segment")
plots.append("Ray")
plots.append("Triangle")
plots.append("Rect")
plots.append("Cycle")
plots.append("CircumCycle")
plots.append("Ellipse")
plots.append("AngleLine")
plots.append("ParalleGram")
plots.append("SpeedResist")
plots.append("FiboFanline")
plots.append("FiboTimezone")
plots.append("Percent")
plots.append("BoxLine")
plots.append("TironeLevels")
plots.append("Parallel")
plots.append("GoldenRatio")
plots.append("LRLine")
plots.append("LRChannel")
plots.append("LRBand")
for i in range(0, len(plots)):
	subView = FCView()
	subView.viewType = "plot"
	subView.text = plots[i]
	subView.viewName = plots[i]
	subView.location = FCPoint(i * 100 + 1, 1)
	subView.size = FCSize(98, 28)
	addViewToParent(subView, layout)
	subView.allowDrag = True
	if subView.paint.defaultUIStyle == "dark":
		subView.backColor = "rgb(0,0,0)"
		subView.borderColor = "rgb(100,100,100)"
		subView.textColor = "rgb(255,255,255)"
	elif subView.paint.defaultUIStyle == "light":
		subView.backColor = "rgb(255,255,255)"
		subView.borderColor = "rgb(150,150,150)"
		subView.textColor = "rgb(0,0,0)"
indicators = []
indicators.append("MA")
indicators.append("BOLL")
indicators.append("MACD")
indicators.append("KDJ")
indicators.append("BIAS")
indicators.append("ROC")
indicators.append("WR")
indicators.append("DMA")
indicators.append("RSI")
indicators.append("BBI")
indicators.append("CCI")
indicators.append("TRIX")
for i in range(0, len(indicators)):
	subView = FCView()
	subView.viewType = "indicator"
	subView.text = indicators[i]
	subView.viewName = indicators[i]
	subView.location = FCPoint(i * 100 + 1, 30)
	subView.size = FCSize(98, 28)
	addViewToParent(subView, layout)
	subView.allowDrag = True
	layout.views.append(subView)
	if subView.paint.defaultUIStyle == "dark":
		subView.backColor = "rgb(0,0,0)"
		subView.borderColor = "rgb(100,100,100)"
		subView.textColor = "rgb(255,255,255)"
	elif subView.paint.defaultUIStyle == "light":
		subView.backColor = "rgb(255,255,255)"
		subView.borderColor = "rgb(150,150,150)"
		subView.textColor = "rgb(0,0,0)"
resetSplitLayoutDiv(split)
current_file_path = os.path.abspath(__file__)
current_file_dir = os.path.dirname(current_file_path)
file0 = open(current_file_dir + "\\SH600000.txt", encoding="UTF-8")
dataText = file0.read()
file0.close()
strs = dataText.split("\n")
strLen = len(strs)
for i in range(2, strLen - 1):
	subStrs = strs[i].split(",")
	if len(subStrs) > 5:			
		data = SecurityData()
		data.date = i
		data.close = float(subStrs[4])
		data.high = float(subStrs[2])
		data.low = float(subStrs[3])
		data.open = float(subStrs[1])
		data.volume = float(subStrs[6])
		chart.data.append(data)

#testDiv4()
calcChartIndicator(chart)
#获取窗体大小
rect = win32gui.GetClientRect(appPaint.hWnd)
#更新布局
appPaint.size = FCSize(rect[2] - rect[0], rect[3] - rect[1])
updateView(appPaint.views)
showWindow(appPaint)